﻿package com.greensock.plugins
{
    import com.greensock.*;
    import com.greensock.core.*;

    public class PhysicsPropsPlugin extends TweenPlugin
    {
        protected var _tween:TweenLite;
        protected var _target:Object;
        protected var _props:Array;
        protected var _hasFriction:Boolean;
        protected var _step:uint;
        protected var _stepsPerTimeUnit:uint = 30;
        public static const API:Number = 1;

        public function PhysicsPropsPlugin()
        {
            this.propName = "physicsProps";
            this.overwriteProps = [];
            return;
        }// end function

        override public function onInitTween(param1:Object, param2, param3:TweenLite) : Boolean
        {
            var _loc_5:String = null;
            var _loc_6:Object = null;
            this._target = param1;
            this._tween = param3;
            this._step = 0;
            var _loc_4:* = this._tween.timeline;
            while (_loc_4.timeline)
            {
                
                _loc_4 = _loc_4.timeline;
            }
            if (_loc_4 == TweenLite.rootFramesTimeline)
            {
                this._stepsPerTimeUnit = 1;
            }
            this._props = [];
            var _loc_7:uint = 0;
            for (_loc_5 in param2)
            {
                
                _loc_6 = param2[_loc_5];
                if (_loc_6.velocity || _loc_6.acceleration)
                {
                    var _loc_10:* = _loc_7 + 1;
                    this._props[_loc_10] = new PhysicsProp(_loc_5, Number(param1[_loc_5]), _loc_6.velocity, _loc_6.acceleration, _loc_6.friction, this._stepsPerTimeUnit);
                    this.overwriteProps[_loc_7] = _loc_5;
                    if (_loc_6.friction)
                    {
                        this._hasFriction = true;
                    }
                }
            }
            return true;
        }// end function

        override public function killProps(param1:Object) : void
        {
            var _loc_2:* = this._props.length;
            while (_loc_2--)
            {
                
                if (this._props[_loc_2].property in param1)
                {
                    this._props.splice(_loc_2, 1);
                }
            }
            super.killProps(param1);
            return;
        }// end function

        override public function set changeFactor(param1:Number) : void
        {
            var _loc_5:PhysicsProp = null;
            var _loc_6:int = 0;
            var _loc_7:Number = NaN;
            var _loc_8:int = 0;
            var _loc_9:Number = NaN;
            var _loc_10:Number = NaN;
            var _loc_2:* = this._props.length;
            var _loc_3:* = this._tween.cachedTime;
            var _loc_4:Array = [];
            if (this._hasFriction)
            {
                _loc_6 = int(_loc_3 * this._stepsPerTimeUnit) - this._step;
                _loc_7 = _loc_3 * this._stepsPerTimeUnit % 1;
                if (_loc_6 >= 0)
                {
                    while (_loc_2--)
                    {
                        
                        _loc_5 = this._props[_loc_2];
                        _loc_8 = _loc_6;
                        while (_loc_8--)
                        {
                            
                            _loc_5.v = _loc_5.v + _loc_5.a;
                            _loc_5.v = _loc_5.v * _loc_5.friction;
                            _loc_5.value = _loc_5.value + _loc_5.v;
                        }
                        _loc_4[_loc_2] = _loc_5.value + _loc_5.v * _loc_7;
                    }
                }
                else
                {
                    while (_loc_2--)
                    {
                        
                        _loc_5 = this._props[_loc_2];
                        _loc_8 = -_loc_6;
                        while (_loc_8--)
                        {
                            
                            _loc_5.value = _loc_5.value - _loc_5.v;
                            _loc_5.v = _loc_5.v / _loc_5.friction;
                            _loc_5.v = _loc_5.v - _loc_5.a;
                        }
                        _loc_4[_loc_2] = _loc_5.value + _loc_5.v * _loc_7;
                    }
                }
                this._step = this._step + _loc_6;
            }
            else
            {
                _loc_9 = _loc_3 * _loc_3 * 0.5;
                while (_loc_2--)
                {
                    
                    _loc_5 = this._props[_loc_2];
                    _loc_4[_loc_2] = _loc_5.start + (_loc_5.velocity * _loc_3 + _loc_5.acceleration * _loc_9);
                }
            }
            _loc_2 = this._props.length;
            if (!this.round)
            {
                while (_loc_2--)
                {
                    
                    this._target[PhysicsProp(this._props[_loc_2]).property] = Number(_loc_4[_loc_2]);
                }
            }
            else
            {
                while (_loc_2--)
                {
                    
                    _loc_10 = _loc_4[_loc_2];
                    this._target[PhysicsProp(this._props[_loc_2]).property] = _loc_10 > 0 ? (int(_loc_10 + 0.5)) : (int(_loc_10 - 0.5));
                }
            }
            return;
        }// end function

    }
}

class PhysicsProp extends Object
{
    public var property:String;
    public var start:Number;
    public var velocity:Number;
    public var acceleration:Number;
    public var friction:Number;
    public var v:Number;
    public var a:Number;
    public var value:Number;

    function PhysicsProp(param1:String, param2:Number, param3:Number, param4:Number, param5:Number, param6:uint)
    {
        this.property = param1;
        var _loc_7:* = param2;
        this.value = param2;
        this.start = _loc_7;
        this.velocity = param3 || 0;
        this.v = this.velocity / param6;
        if (param4 || param4 == 0)
        {
            this.acceleration = param4;
            this.a = this.acceleration / (param6 * param6);
        }
        else
        {
            var _loc_7:int = 0;
            this.a = 0;
            this.acceleration = _loc_7;
        }
        this.friction = param5 || param5 == 0 ? (1 - param5) : (1);
        return;
    }// end function

}

